package com.iot.core;

import java.util.List;

public class LResult<T> {
    /// <summary>
    /// 前台Ajax请求的统一返回结果类
    /// </summary>
    private LResult() {
    }

    /// <summary>
    /// 是否产生错误
    /// </summary>
    public int errcode=0;

    /// <summary>
    /// 错误信息，或者成功信息
    /// </summary>
    public String message="";

    /// <summary>
    /// 成功可能时返回的数据
    /// </summary>
    public T data=null;
    public long count=0;
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public static LResult Error() {
        LResult lr = new LResult();
        lr.errcode = -1;
        lr.message="";
        lr.count=0;
        lr.data=null;
        return lr;
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public static LResult Error(String message) {
        LResult lr = new LResult();
        lr.errcode = -1;
        lr.message = message;
        lr.count=0;
        lr.data=null;
        return lr;

    }
    public static LResult forbidden() {
        LResult lr = new LResult();
        lr.errcode = 401;
        lr.message = "Token过期";
        return lr;
    }
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public static LResult Success() {
        LResult lr = new LResult();
        lr.errcode = 0;
        lr.message="";
        return lr;
    }

    public static<T> LResult Success(T data) {
        LResult lr = new LResult();
        lr.errcode = 0;
        lr.data = data;
        lr.message="";
        return lr;
    }
    public static  <T>  LResult Success(long count, List<T> data) {
        LResult lr = new LResult();
        lr.count=count;
        lr.errcode = 0;
        lr.data = data;
        lr.message="";
        return lr;
    }
    public String toJson() {
        return JsonHelper.toJson(this);
    }

    //        public Override string toString()
//    {
//
//        JsonHelper json = new JsonHelper();
//
//
//        return json.SerializeObject(this);
//        //return   new JavaScriptSerializer().Serialize(this);
//
//    }
}
